﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using ConfigAndElse;

public partial class main_comm_FileDownload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string FilePathCode = Request.QueryString["PathCode"];

        if (FilePathCode == null || FilePathCode == string.Empty)
        {
            FilePathCode = "SAVE_FILE_PATH";
        }

        string FileName = Request.QueryString["FileName"];
        //ファイルダウンロードダイアログを表示する
        FileInfo DownloadFile;
        if (FilePathCode == "SAVE_FILE_PATH")
        {
            DownloadFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings[FilePathCode]) + FileName);

        }
        else
        {
            DownloadFile = new FileInfo(SystemConfig.Config[FilePathCode] + FileName);
        }
        if (DownloadFile.Exists == true)
        {
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename="
                + HttpUtility.UrlEncode(DownloadFile.Name, Encoding.UTF8));
            Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
            Response.WriteFile(DownloadFile.FullName);
            Response.Flush();
            Response.End();
        }
        else
        {
            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.AppendFormat("alert(\"");
            sbr.AppendFormat("没有找到指定文件。");
            sbr.AppendFormat("\");window.history.back();</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
        }
    }
}
